function getRules(rules) {
    const arr = rules.split('|');
    const obj = {};
    arr.forEach((rule) => {
        const list = rule.split(':');
        if (list.length > 1) {
            obj[list[0]] = list[1];
        } else {
            obj[list[0]] = true;
        }
    });
    return obj;
}
export default {
    mounted(el, binding, vnode, prevVnode) {
        const rules = el.dataset['rules'];
        const errorfn = binding.value;
        if (!rules) return;
        const inrules = getRules(rules);
        el.addEventListener('input', (e) => {
            const value = e.target.value;
            let error;
            if ('email' in inrules) {
                const str =
                    /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/;
                if (!str.test(value)) {
                    error = `请输入正确的邮箱格式`;
                }
            }
            if ('minLength' in inrules) {
                if (value.length < inrules['minLength']) {
                    error = `输入长度至少为${inrules['minLength']}位`;
                }
            }
            if ('required' in inrules) {
                if (!value) {
                    error = '必填项';
                }
            }
            errorfn(error);
        });
    },
}

